<script>
	var typeTest = "{$smarty.get.type|default:"time"}";
</script>
{literal}
<link rel="stylesheet" title="Visual Studio" href="templates/js/highlight/styles/idea.css">
<script src="templates/js/highlight/highlight.pack.js"></script>
<script>
	var testQueue = [];
	var queueIter = 0;
	var testResults = {};
	var cf = {
		"time": [1000000, 1000],
		"memory": [1, 1000]
	}
	hljs.initHighlightingOnLoad();
	function bColorize(sel, per) {
		if(per < 120) {
			$(sel).addClass("per-1");
		} else if (per >= 120 && per < 175) {
			$(sel).addClass("per-2");
		} else if (per >= 175 && per < 250) {
			$(sel).addClass("per-3");
		} else if (per >= 250 && per < 750) {
			$(sel).addClass("per-4");
		} else {
			$(sel).addClass("per-5");
		}
	}
	
	function addItem(test, script, bench) {	
		testQueue.push(function () {
			if(!testResults[script]) {
				$.get("?bench="+script+"&json&"+Math.random(), function (data) {
					try {
						testResults[script] = eval("("+data+")");
					} catch(e) {
						$(".bench-val-"+test, "#bench-"+bench).html("<span class='error'>Error</span>");
						runQueueItem();
						return;
					}
					if(!!testResults[script][test]) {
						$("#val-"+test).val(testResults[script][test][typeTest]);
						testResults[script][test][typeTest] = Math.round(parseFloat(testResults[script][test][typeTest]) * cf[typeTest][0])/cf[typeTest][1];
						$(".bench-val-"+test, "#bench-"+bench).html(testResults[script][test][typeTest] + (typeTest == "time" ? " ms" : " KB"));
					}
					runQueueItem();
				}, "text");
			} else {
				if(!!testResults[script][test]) {
					$("#val-"+test, "#bench-"+bench).val(testResults[script][test][typeTest]);
					testResults[script][test]["time"] = Math.round(parseFloat(testResults[script][test][typeTest]) * cf[typeTest][0])/cf[typeTest][1];
					$(".bench-val-"+test, "#bench-"+bench).html(testResults[script][test][typeTest] + (typeTest == "time" ? " ms" : " KB"));
				}
				runQueueItem();
			}
		});
	}
	
	function runQueue() {
		testQueue.push(function () {
			setPercents();
			colorizePercents();
		});
		runQueueItem();
	}
	
	function runQueueItem() {
		if(!testQueue[queueIter]) {
			alert(123);
			return;
		}
		testQueue[queueIter++].call(this);
	}
	
	function setPercents() {
		var $inp = $("input.val-result").each(function (i, e) {
			$("#bench-per-" + e.title)
		});
		var totalTime = 1000000000.12;
		for(var i=0; i<$inp.length; i++) {
			var time = parseFloat($inp.eq(i).val());
			if(time && time < totalTime) 
				totalTime = time;
		}
		for(var i=0; i<$inp.length; i++) {
			var per_time = Math.round( parseFloat($inp.eq(i).val())/totalTime*100);
			$("#bench-per-" + $inp.eq(i).attr("title")).html(per_time + " %");
			$("#val-percent-" + $inp.eq(i).attr("title")).val(per_time);
		}
	}
	
	function colorizePercents() {
		var $inp = $("input.val-percent-result").each(function (i, e) {
			bColorize("#bench-per-" + e.title, e.value);
		});
	}
</script>
<style>
	pre {
		margin: 0;
	}
	.bench-code {
		border: 1px solid #DCDDFF;
		padding: 3px 5px;
	}
	.show-code {
		font-style: italic;
		display: block;
	}
	.show-code-i {
		display: inline;
	}
	
	h1 {
		font-size: 18px;
	}
	
	.bench-list {
		margin: 5px 3px;
		border-top: dashed 1px #DCDDFF;
		border-bottom: dashed 1px #DCDDFF;
		padding: 2px 4px;
	}
	
	.bench-item {
		border-top: dotted 1px #DCDDFF;
		border-bottom: dotted 1px #DCDDFF;
		margin: 3px 2px;
		padding: 4px 4px;
	}
	
	.bench-desc {
		float: left;
		width: 75%;
	}
	
	.bench-result {
		float:right;
		width: 25%;
	}
	
	.bench-val {
		float: right;
		font-weight: bold;
		font-size: 110%;
		padding: 3px 5px;
	}
	
	.bench-per {
		float: right;
		font-weight: bold;
		font-size: 105%;
		padding: 2px 4px;
	}
	
	div.source-code {
		float: right;
		font-size: 85%;
	}
	
	.source-code a, span.source-code {
		font-size: 85%;
	}
	
	.env {
		text-align: right;
		font-size: 95%;
		color: #888888;
		border-bottom: 1px solid #DCDDFF;
		margin: 1px 4px;
	}
	
	.per-1 {
		border: 1px solid green;
		background-color: #B9FFB8;
		color: green;
	}
	
	
	.per-2 {
		border: 1px solid #79F200;
		background-color: #D2F9AB;
		color: #79F200;
	}
	.per-3 {
		border: 1px solid #CDCD4A;
		background-color: #FEFFB8;
		color: #CDCD4A;
	}
	
	.per-4 {
		border: 1px solid orange;
		background-color: #FFE7B8;
		color: orange;
	}
	
	.per-5 {
		border: 1px solid red;
		background-color: #FFC8B8;
		color: red;
	}
</style>
{/literal}
<div class="env">{$smarty.now|date_format:"%Y-%m-%d %T"}, php {$smarty.const.PHP_VERSION}, {$smarty.const.PHP_OS}</div>
<div  class="source-code"><a href="?code={$bench.code}">Исходный код теста</a> | <a href="?clean={$bench.code}">Пересобрать</a> | {if $smarty.get.type=="memory"}<a href="?bench={$bench.code}">Показывать затраченое время</a>{else}<a href="?bench={$bench.code}&type=memory">Показывать затраченую память</a>{/if}</div>
<h1>{$bench.name}</h1>
{$bench.desc|nl2br}
{if $bench.prepare || $bench.script_prepare}
	<a href="javascript: void(0);" class="show-code" onclick="$(this).hide('normal');$('#pre-code').slideDown('normal');">Показать предварительный код</a>
	<div id="pre-code" class="bench-code" style="display:none;">
	<pre><code class="php">{$bench.prepare}</code></pre>
	{if $bench.script_prepare}{foreach from=$bench.script_prepare item=e}
	...
	<pre><code class="php">{$e}</code></pre>
	{/foreach}{/if}
	</div>
{/if}

{assign var='min' value=10000}
{foreach from=$result item=b}
	{if $min>$b.time}{assign var='min' value=`$b.time`}{/if}
{/foreach}

<div class="bench-list" id="bench-{$bench.code}">
	{foreach from=$bench.tests item=t key=k}
	<div class="bench-item">
		<div class="bench-desc">
			<b>{$t.desc}</b> {if $t.i > 1}<i>x{$t.i}</i>{/if} {if $t.script}<span class="source-code">(<a href="?code={$t.script}">готовый код</a>)</span>{/if}
			{if $t.code}
				<a href="javascript: void(0);" class="show-code" onclick="$(this).hide('normal');$('#bench-code-{$k}').slideDown('normal');">Показать код</a>
				<div id="bench-code-{$k}" class="bench-code" style="display:none;">
					{if $t.prepare}<pre><code class="php">{$t.prepare} // prepare</code></pre> ...{/if}
					<pre><code class="php">{$t.code} {if $t.i > 1}// x{$t.i} times{/if}</code></pre>
				</div>
			{/if}
		</div>
		<div class="bench-result">
			<span class="bench-per" id="bench-per-{$k}">--- %</span>
			<input type="hidden" class="val-percent-result" id="val-percent-{$k}" title="{$k}" value="" />
			<span class="bench-val bench-val-{$k}">
			{if $result[$k].time}{$result[$k].time*1000|round:3} ms
			{else}<img src="templates/css/images/loader.gif" />{/if}
			</span>
			<input type="hidden" class="val-result" id="val-{$k}" title="{$k}" value="{$result[$k].time*1000}" />
			
			{if $t.script}
				<script>addItem('{$k}', '{$t.script}', '{$bench.code}');</script>
			{/if}
		</div>
		<br clear="both" />
	</div>
	{/foreach}
	<script>runQueue();</script>
</div>
